package renguanyu.app.auth4fx.controller.permission;

import javafx.concurrent.Task;
import renguanyu.app.auth4fx.model.Page;
import renguanyu.app.auth4fx.model.permission.PermissionDto;
import renguanyu.app.auth4fx.model.permission.PermissionVo;
import renguanyu.app.auth4fx.service.permission.PermissionService;
import renguanyu.app.auth4fx.util.PageUtil;
import renguanyu.app.auth4fx.view.permission.PermissionListView;

import java.util.List;

public class PermissionListQueryController extends Task<Void> {

    private PermissionListView permissionListView;
    public PermissionListQueryController(PermissionListView permissionListView){
        this.permissionListView = permissionListView;
    }

    public void execute() {
        System.out.println("PermissionListQuery.execute");
        Thread thread = new Thread(this);
        thread.setDaemon(true);
        thread.start();
    }

    Page page;
    @Override
    protected Void call() {
        System.out.println("PermissionListQuery.call");
        PermissionDto permissionDto = buildPermissionDto();
        page = PermissionService.getPermissionPage(permissionDto);
        return null;
    }

    private PermissionDto buildPermissionDto() {
        // 接收参数
        int pageSize = permissionListView.pageSizeProperty.get();
        int currentPageIndex = permissionListView.currentPageIndexProperty.get();
        String searchKey = permissionListView.searchFieldKey.getValue();
        String searchValue = permissionListView.searchFieldValue.getValue();
        // 格式化
        String name = "";
        String password = "";
        if ("权限名".equals(searchKey) && searchValue!=null) {
            name = "%"+searchValue+"%";
        }
        PermissionDto permissionDto = new PermissionDto();
        permissionDto.setPageSize(pageSize);
        permissionDto.setCurrentPageIndex(currentPageIndex);
        permissionDto.setName(name);
        return permissionDto;
    }


    @Override
    protected void running() {
        System.out.println("PermissionListQuery.running");
        permissionListView.dataPaneStatusLoading();
        permissionListView.dataPaneObservableList.clear();
        permissionListView.selectAllBooleanProperty.set(false);
    }

    @Override
    protected void succeeded() {
        System.out.println("PermissionListQuery.succeeded");
        // 无数据
        if(page.getPageData().isEmpty()){
            permissionListView.dataPaneStatusNoData();
            permissionListView.pageCountProperty.set(1);
            permissionListView.maxPageIndicatorCount.set(1);
            return;
        }
        // 有数据
        Integer pageCount = page.getPageCount();
        permissionListView.pageCountProperty.set(pageCount);
        permissionListView.maxPageIndicatorCount.set(PageUtil.getMaxPageIndicatorCount(pageCount));
        permissionListView.dataPaneObservableList.setAll(convertToPermissionVoList());
    }

    private List<PermissionVo> convertToPermissionVoList() {
        List<PermissionVo> permissionList = page.getPageData().stream().map(e -> {
            PermissionVo permission = new PermissionVo();
            permission.setId(e.get("id").toString());
            permission.setName(e.get("name").toString());
            return permission;
        }).toList();
        return permissionList;
    }

    @Override
    protected void failed() {
        System.out.println("PermissionListQuery.failed");
        permissionListView.dataPaneStatusError();
        permissionListView.pageCountProperty.set(1);
        permissionListView.maxPageIndicatorCount.set(1);
    }

}
